package org.anderes.playground.jaxb;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.anderes.playground.jaxb.data.LinkType;
import org.anderes.playground.jaxb.data.ObjectFactory;
import org.anderes.playground.jaxb.data.RecipeListType;
import org.anderes.playground.jaxb.data.RecipeShortType;
import org.junit.Before;
import org.junit.Test;

public class RecipesJaxb {
    
    private ObjectFactory of;
    private RecipeListType recipes;

    @Before
    public void setup() {
        of = new ObjectFactory();
    }

    @Test
    public void recipesTest() {
        recipes = createRecipeList(); 
        marshal(recipes);
    }
    
    private RecipeListType createRecipeList() {
        RecipeListType recipes = of.createRecipeListType();
        RecipeShortType recipe = of.createRecipeShortType();
        recipe.setId("Rezept-ID_096543432aa08");
        recipe.setTitle("Test");
        LinkType link = of.createLinkType();
        link.setRel("/linkrels/recipes/" + recipe.getId());
        link.setUri("/recipes/" + recipe.getId());
        recipe.setLink(link);
        recipes.getRecipeShort().add(recipe);
        return recipes;
    }
   
    private void marshal(RecipeListType recipes) {
        JAXBElement<RecipeListType> recipeList = of.createRecipes(recipes);
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(RecipesJaxb.class.getPackage().getName() + ".data");
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "iso-8859-1");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(recipeList, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}
